
import os
import shutil
import logging
import tempfile
import unittest

import pluginlib

class  LoadingTestCase(unittest.TestCase):
    def setUp(self):
        self.dir = tempfile.mkdtemp(prefix='pluginlib-test')
        self.mgr = pluginlib.manager.PluginManager([])

    def tearDown(self):
        shutil.rmtree(self.dir)

    def build_plugin(self, content):
        cfg_file = os.path.join(self.dir, 'plugin.conf')
        module_file = os.path.join(self.dir, 'test.py')
        with open(cfg_file, 'w') as f:
            f.write('[Plugin]\n')
            f.write('name: test\n')
            f.write('version: 1.0\n')
            f.write('load modules: test\n')
        with open(module_file, 'w') as f:
            f.write(content)
        return pluginlib.plugin.Plugin.create(self.mgr, 'dir', self.dir)

    def test_syntax_error(self):
        plug = self.build_plugin('bad line:')

        self.assertRaises(pluginlib.plugin.PluginLoadingError, plug.load)

    def test_raise_exception(self):
        plug = self.build_plugin('raise RuntimeError()')

        self.assertRaises(pluginlib.plugin.PluginLoadingError, plug.load)

    def test_ok(self):
        plug = self.build_plugin('def load(ctx):\n'
                                 '    pass\n')

        plug.load()

if __name__ == '__main__':
    unittest.main()

